import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.JDBC4PreparedStatement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

class Messagess{
    public String from;
    public String to;
    public String message;
}
@WebServlet("/practiceD")
public class PracticeDatabase extends HttpServlet {
    ObjectMapper mapper=new ObjectMapper();
    DataSource dataSource=new MysqlDataSource();
    public void init(){
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java112?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Messagess messagess=mapper.readValue(req.getInputStream(),Messagess.class);

        System.out.println("收到的数据 ： " +messagess);
        try {
            save(messagess);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        resp.setStatus(200);
        resp.getWriter().write("ok");
    }

    private void save(Messagess messagess) throws SQLException {
        Connection connection= (Connection) dataSource.getConnection();

        String request="insert into confession values(?,?,?) ";
        PreparedStatement statement=connection.prepareStatement(request);
        statement.setString(1,"from");
        statement.setString(2,"to");
        statement.setString(3,"message");

        statement.executeUpdate();

        connection.close();
        statement.close();
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(200);
        List<Messagess> messagessList=null;
        try {
            messagessList=load();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.setContentType("application/json;charset=utf8");
        String respJson=mapper.writeValueAsString(messagessList);
        resp.getWriter().write(respJson);
    }

    private List<Messagess> load() throws SQLException {
        Connection connection= (Connection) dataSource.getConnection();

        String sql="select * from confession";
        PreparedStatement statement= connection.prepareStatement(sql);

        ResultSet resultSet =statement.executeQuery();
        List<Messagess> list=new LinkedList<>();
        while(resultSet.next()){
            Messagess messagess=new Messagess();
            messagess.from=resultSet.getString("from");
            messagess.to=resultSet.getString("to");
            messagess.message=resultSet.getString("message");
            list.add(messagess);
        }

        resultSet.close();
        connection.close();
        statement.close();
        return list;
    }
}
